﻿<?php
/**
 * AdminController
 * 
 * @category   Longadmin
 * @package    Longadmin_Controllers
 */

class Longadmin_AdminController extends Zend_Controller_Action 
{
	/**
	 * The default action - show the home page
	 */
	protected $_PATH = "/home/vhosts/shoppingcartcnpm.orgfree.com/public/images/";
	protected $_IMAGE ;
	public function indexAction() 
	{
        if (!$this->_helper->acl('Admin')) {
            throw new SF_Acl_Exception('Access Denied');
        }
	}
	
	public function homeAction()
	{
		// phan ve Category 
		$db = Zend_Registry::get('db');
		$table = 'category';
		
		$select = $db->select()->from($table);
		$category = $db->query($select)->fetchAll();

		$this->view->category = $category;
	
		
		// phan ve Supplier
		$table = 'supplier';
		$select = $db->select()->from($table);
		$suppliers = $db->query($select)->fetchAll();
		
		//$CategoryModel = new Model_Category();
		//$category = $CategoryModel->listAllCategory();
		$this->view->suppliers = $suppliers;		
		$this->view->addSuppliersForm = $form;
		
	}
	
	public function addAction()
	{	
		$form = new Zend_Form();
		$form->setAction('add') 
			 ->setMethod('post');
		$form->addElement('text','s_tenText',array(
            'required'   => true,
            'label'      => 'Hãng sản xuất:',
        ));
		$form->addElement('text','s_diachiText',array(
            'required'   => true,
            'label'      => 'Xuất xứ:',
        ));
		
		$form->addElement('text','s_linkText',array(
            'required'   => false,
            'label'      => 'Trang chủ:',
        ));		
		
		$element = new Zend_Form_Element_File('image');
		$element->setLabel('Upload ảnh nhà cung cấp:')
				->setDestination("/home/vhosts/shoppingcartcnpm.orgfree.com/public/images/suppliers");
		$element->addValidator('Extension',false,'jpg,png,gif');
		$form->addElement($element,'image');
		$form->addElement('submit','Tạo mới');
		$form->setAttrib('enctype', 'multipart/form-data');
		if($form->image->receive()){
			$_IMAGE = $form->image->getFileName(null,false); 
		}
		$this->view->message = $form;
		if($this->_request->isPost()){
			$db = Zend_Registry::get('db');
			$table = 'supplier';
			$select = $db->select()->from($table)->where('s_ten= ?',$_POST['s_tenText']);
			$search = $db->query($select)->fetchAll();
			if(count($search) ==0){
				$params = array('s_ten'=>$_POST['s_tenText'],
							's_diachi'=> $_POST['s_diachiText'],
							's_link' => $_POST['s_linkText'],
							's_anh' => $_IMAGE);
				$db->insert($table,$params);
				$this->view->message = 'Hoàn Thành, Đã Tạo Thêm Nhà Cung Cấp  '.$_POST['s_tenText'];
			}
			else{ 
				$this->view->message = 'Đã có nhà cung cấp '.$_POST['s_tenText'];
			}
		}
	}
	
	public function testAction()
	{

	}
	
	public function chatAction()
	{
	
	}
	
	public function windowAction()
	{
	
	}
	
}
